# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class AccountChartTemplate(models.Model):
    _inherit = "account.chart.template"

    # odoo 15 和  odoo 16  写法不一样
    def _load(self, *args):
        from odoo import release
        version = release.version_info[0]
        if version >= 16:
            return self._load_for_odoo16(*args)
        else:
            return self._load_for_odoo15(*args)

    def _load_for_odoo16(self, company):
        # 创建新公司, 加载会计科目表时, 修正 transfer_account_id
        super_ret = super(AccountChartTemplate, self)._load(company)

        # Set the transfer account on the company
        self._set_company_transfer_account(company)

        return super_ret

    def _load_for_odoo15(self, sale_tax_rate, purchase_tax_rate, company):
        # 创建新公司, 加载会计科目表时, 修正 transfer_account_id
        super_ret = super(AccountChartTemplate, self)._load(
            sale_tax_rate, purchase_tax_rate, company)

        # Set the transfer account on the company
        self._set_company_transfer_account(company)

        return super_ret

    def _set_company_transfer_account(self, company):
        # odoo 官方代码, 查找 transfer_account 时, 忽略了 reconcile=True 这个条件, 会取错科目
        company.transfer_account_id = self.env['account.account'].search([
            ('code', '=like', self.transfer_account_code_prefix + '%'),
            ('reconcile', '=', True),
            ('company_id', '=', company.id)], limit=1)

    # def generate_account(self, tax_template_ref, acc_template_ref, code_digits, company):

    #     res = super().generate_account(
    #         tax_template_ref, acc_template_ref, code_digits, company)

    #     print('xxxx,generate_account,', res)

    #     return res
